<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Classes extends User_Controller {

	public function __construct()
    {
        parent::__construct();
        // Your own constructor code
        $this->smarty->assign("current_class", $this->router->fetch_class());
        $this->smarty->assign("current_method",$this->router->fetch_method());
		$this->uid = $this->user_data['id'];
		if( $this->logged_in==0) {header("Location:/");exit;}
    }

    public function index($status = 0, $class_name = 0, $course_name = 0, $teacher_name = 0, $plan_time = 0, $limit = 0)
	{
		$post_data = $this->input->post();
		if($post_data){
			$limit = 0;
			$class_name = !empty($post_data['class_name']) ? urldecode($post_data['class_name']) : 0;
			$course_name = !empty($post_data['course_name']) ? urldecode($post_data['course_name']) : 0;
			$teacher_name = !empty($post_data['teacher_name']) ? urldecode($post_data['teacher_name']) : 0;
			$plan_time = !empty($post_data['plan_time']) ? $post_data['plan_time'] : 0;
		}
		$this->smarty->assign("action","我的班级");
		//获取班级
		$this->load->model('User_center_model');
		$conditions['uid'] = $this->user_data['id'];
		$conditions['status'] = $status;
		$conditions['class_name'] = $class_name;
		$conditions['course_name'] = $course_name;
		$conditions['teacher_name'] = $teacher_name;
		$conditions['plan_time'] = $plan_time;
		// print_r($conditions);
		$classes = $this->User_center_model->get_class_by_uid($limit, $this->offset, $conditions);
		$this->smarty->assign("class_list", $classes['result']);

		$this->load->library('pagination');
		$page_config['base_url'] = '/user/classes/index/' . $status . '/' . $class_name . '/' . $course_name . '/' . $teacher_name . '/' . $plan_time;
		$page_config['total_rows'] = $classes['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 9;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		// print_r($classes);exit;
		// print_r($conditions);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("conditions", $conditions);
		$this->smarty->assign("menu", "classes");
		$this->smarty->display("user/class_list.html");
	}

	/**
	 * 班级排课
	 * @param  string $value [description]
	 * @return [type]        [description]
	 */
	public function plan($class_id)
	{
		//班级详情
		$this->load->model('User_center_model');
		$class_info = $this->User_center_model->get_class_by_id($class_id);
		$this->smarty->assign("class_info", $class_info);


		$this->load->model("Class_plan_model");
		$plan_list = $this->Class_plan_model->get_plan_by_class($class_id);
		//获取排课list
		$this->smarty->assign("plan_list", $plan_list);

		$this->smarty->assign("menu", "classes");
		$this->smarty->display("user/class_plan.html");

	}


	public function detail($id){
		//班级详情
		$this->load->model('User_center_model');
		$class_info = $this->User_center_model->get_class_by_id($id);
		$this->smarty->assign("class_info", $class_info);

		//获取班级学员
		$this->load->model('User_center_model');
		$student = $this->User_center_model->get_student_by_class($id);
		$this->smarty->assign("student", $student);
		$this->smarty->assign("menu", "classes");
		$this->smarty->display("user/class_detail.html");
	}

	function join_class(){
		$post_data = $this->input->post();
		if($post_data){
			//查询code
			$this->load->model('Offline_class_model');
			$result = $this->Offline_class_model->get_class_by_code($post_data['code'],$this->school_id);
			if($result>0){
				//加入班级
				$this->Offline_class_model->join_class($this->user_data['id'],$result);
				header("Location:/user/classes");exit;
			}else{
				$this->smarty->assign("errmsg", "找不到班级");
				$this->smarty->display("user/class_join.html");
			}

		}
		$this->smarty->display("user/class_join.html");
	}

	function volume_list($type = 3, $subject_id = 0, $home_work_name = 0, $limit = 0){
		$this->left_menu_min['curr'] = '/tiku/test_volume';
		$this->offset = 10;
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post();
			$home_work_name = $post_data['home_work_name'] ? urldecode($post_data['home_work_name']) : 0;
			$subject_id = isset($post_data['subject_id']) ? $post_data['subject_id'] : 0;
			$type = isset($post_data['type']) ? $post_data['type'] : 3;
		}
		$this->load->model("Topics");
		$conditions = array('subject_id' => $subject_id, 'home_work_name' => $home_work_name, "type" => $type);
		$this->load->model("Test_volumes");
		$result = $this->Test_volumes->get_volume_publish_list($limit, $this->offset, $conditions);
		$this->load->library('pagination');
		$page_config['base_url'] = '/user/classes/volume_list/' . $type . '/' . $subject_id . '/' . $home_work_name;
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 6;

		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$subject = $this->Topics->get_subject();
		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("test_volume_list", $result['result']);
		$this->smarty->assign("conditions", $conditions);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("time", time());
		$this->left_menu_min['curr'] = '/classes/volume_list/'.$type;
		$this->smarty->assign("left_menu_min", $this->left_menu_min);
		$this->smarty->assign("type", $type);
		$this->smarty->assign("menu", 'classes');
		$this->smarty->display("classes/class_home_work_list.html");
	}

	function volume_historys($home_work_id = 0, $type = 3, $name = 0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post();
			$home_work_id = !empty($post_data['home_work_id']) ? $post_data['home_work_id'] : 0;
			$name = !empty($post_data['name']) ? urldecode($post_data['name']) : 0;
			$type = !empty($post_data['type']) ? $post_data['type'] : 0;
		}
		if($home_work_id > 0){
			$volume_info = get_field_by_where("tk_test_volumes_publish", "*", "id='{$home_work_id}' AND school_id = '{$this->school_id}'");
			if(!$volume_info){
				show_message($this->jump_url, "数据不存在");exit;
			}

			$this->load->model("Test_volumes");
			$conditions['home_work_id'] = $home_work_id;
			$conditions['name'] = $name;
			$conditions['type'] = $type;
			$history_list = $this->Test_volumes->get_volume_publish_history($limit, $this->offset, $conditions);

			$this->load->library('pagination');
			$page_config['base_url'] = '/classes/volume_historys/' . $home_work_id . '/' . $type . '/' . $name;
			$page_config['total_rows'] = $history_list['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 6;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("menu", "classes");
			$this->smarty->assign("type", $volume_info['type']);
			$this->smarty->assign("history_list", $history_list['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->display("classes/class_home_work_history.html");
		}else{
			show_message($this->jump_url, "非法操作");exit;
		}

	}

	function show_volume($home_work_history_id = 0, $type = 3){
		$home_work_history = get_field_by_where("tk_test_volume_historys", "*", "id='{$home_work_history_id}'");
		// print_r($home_work_history);
		if(!$home_work_history){
			show_message($this->jump_url, "数据不存在");exit;
		}

		$home_work_info = get_field_by_where("tk_test_volumes", "id, topic_info, template_id", "id='{$home_work_history['volume_id']}'");
		if(!$home_work_info){
			show_message($this->jump_url, "作业数据不存在");exit;
		}
		$template_info = get_field_by_where("tk_test_volume_templates", "*", "id='{$home_work_info['template_id']}'");
		$template_content = json_decode($template_info['content'], true);
		$this->smarty->assign("template_content", $template_content);

		$home_work_info['uid'] = $home_work_history['uid'];
		$home_work_info['home_work_id'] = $home_work_history['volume_id'];
		$home_work_info['data_id'] = $home_work_history['data_id'];
		$this->load->model("Topics");
		$result = $this->pretend_topics($home_work_info, true, $type, $home_work_history_id, true);
		$topic_list = $result['topic_list'];
		$topic_type_analys = $result['topic_type_analys'];
		
		$letter = array('A','B','C','D');
		// print_r($topic_list);exit;
		$home_work_info['percentage'] = $home_work_history['percentage'];
		$home_work_info['right_percentage'] = $home_work_history['right_percentage'];
		$home_work_info['fraction'] = $home_work_history['fraction'];
		$home_work_info['begin_time'] = $home_work_history['begin_time'];
		$home_work_info['end_time'] = $home_work_history['end_time'];
		$home_work_info['timeout_time'] = $home_work_history['timeout_time'];
		$home_work_info['status'] = $home_work_history['status'];
		$this->smarty->assign("test_volume_data", $home_work_info);
		$this->smarty->assign("topic_list", $topic_list);
		$this->smarty->assign("topic_type_analys", $result['topic_type_analys']);
		/*print_r($home_work_info);*/
		// print_r($topic_list);
		$this->smarty->assign("letter", $letter);
		$this->smarty->assign("home_work_history_id", $home_work_history_id);
		$this->smarty->assign("type", $type);
		$this->smarty->display("classes/class_show_home_work.html");
	}

	//组装题目
	function pretend_topics($test_home_work = array(), $need_user_result = false, $type = 3, $home_work_history_id = 0, $need_topic_type_analys = false){
		$topic_list = array();
		$topic_type_analys = array(
								"1" => array("name" => "单选题", "right_num" => 0, "wrong_num" => 0, "fraction" => 0),
								"2" => array("name" => "多选题", "right_num" => 0, "wrong_num" => 0, "fraction" => 0),
								"3" => array("name" => "判断题", "right_num" => 0, "wrong_num" => 0, "fraction" => 0),
								"4" => array("name" => "计算分析题", "right_num" => 0, "wrong_num" => 0, "fraction" => 0),
								"7" => array("name" => "不定向选择题", "right_num" => 0, "wrong_num" => 0, "fraction" => 0)
						 		);
		if($test_home_work){
			$topic_info_list = $this->Topics->get_topic_by_id($test_home_work['topic_info']);
			if($topic_info_list){
				foreach ($topic_info_list as $key => $value) {
					$user_answer = '';
					if($need_user_result){
						$where = "uid = '{$test_home_work['uid']}' AND volume_id='{$test_home_work['home_work_id']}' AND data_id = '{$test_home_work['data_id']}' AND topic_id='{$value['id']}' AND type = '{$type}' ";
						if($home_work_history_id > 0){
							$where .= " AND volume_history_id='{$home_work_history_id}'";
						}
						$user_answer = get_field_by_where("tk_test_topic_historys", "*", $where);
						if($user_answer && $need_topic_type_analys){
							if($user_answer['topic_type'] < 8){
								if($user_answer['is_right'] == 1){
									$topic_type_analys[$user_answer['topic_type']]['right_num'] = $topic_type_analys[$user_answer['topic_type']]['right_num'] + 1;
									$topic_type_analys[$user_answer['topic_type']]['fraction'] = $topic_type_analys[$user_answer['topic_type']]['fraction'] + $user_answer['fraction'];
								}else{
									$topic_type_analys[$user_answer['topic_type']]['wrong_num'] = $topic_type_analys[$user_answer['topic_type']]['wrong_num'] + 1;
								}
							}
						}
						if($value['type'] == 4 || $value['type'] == 7){
							$user_answer = json_decode($user_answer['user_answers'], true);
						}elseif($value['type'] == 8 || $value['type'] == 9){
							$user_answer = array("user_answers"=>$user_answer['user_answers'],"fraction"=>$user_answer['fraction'],"feedback"=>$user_answer['feedback']);
						}else{
							$user_answer = explode(",", $user_answer['user_answers']);
						}
					}

					if($value['type'] == 4){
						$analy_result = $this->Topics->get_topic_analys($value['id']);
						if($analy_result){
							$value['result'][$key] = $analy_result;
							foreach ($analy_result as $k => $v) {
								$answer_list = $this->Topics->get_topic_analys_result($v['id']);
								$value['result'][$key][$k]['answer_list'] = $answer_list;
								if($need_user_result){
									$value['result'][$key][$k]['user_answer_list'] = $user_answer ? $user_answer[$v['id']] : '';
								}
							}
							$topic_list[$value['type']][] = $value;
						}
					}else{
						$value['result'] = $this->Topics->get_topic_result_by_id($value['id']);
						if($need_user_result){
							$value['user_answer'] = $user_answer ? $user_answer : '';
						}
						$topic_list[$value['type']][] = $value;
					}
				}
			}
		}
		if($need_topic_type_analys){
			return array("topic_list" => $topic_list, "topic_type_analys" => $topic_type_analys);	
		}else{
			return array($topic_list);
		}
	}

	function mark_volume(){
		$post_data = $this->input->post();
		if(!empty($post_data['volume_history_id']) && !empty($post_data['topic_id'])){
			$this->db->where("topic_id", $post_data['topic_id']);
			$this->db->where("volume_history_id", $post_data['volume_history_id']);
			$this->db->update("tk_test_topic_historys", array("fraction"=>$post_data['fraction'], "feedback" => $post_data['feedback']));
			if($this->db->affected_rows() > 0){
				echo 1;
			}else{
				echo 0;
			}
		}else{
			echo 2;
		}
	}

	function update_score(){
	    $post_data = $this->input->post();
	    if(empty($post_data['teacher_id'])){
	      $result['error'] = 1;
	      $result['msg'] = '请选择一个老师';
	      echo json_encode($result);exit;
	    }
	    if(empty($post_data['score'])){
	      $result['error'] = 1;
	      $result['msg'] = '请填写分数';
	      echo json_encode($result);exit;
	    }

	    $this->load->model('Student_model');
	    $student = $this->Student_model->get_student_by_userid($this->user_data['id']);
	    $score_data['class_id'] = $post_data['class_id'];
	    $score_data['student_id'] = $student['student_id'];
	    $score_data['teacher_id'] = $post_data['teacher_id'];
	    $score_data['score'] = $post_data['score'];
	    $score_data['content'] = $post_data['content'];
	    $score_data['add_time'] = time();
	    $score_data['type'] = 2;
	    $this->db->insert("o2o_school_student_scores", $score_data);
	    $result['error'] = 0;
	    $result['msg'] = '评分成功';
	    echo json_encode($result);exit;
	   
  	}

  	function courseware($class_id = 0, $name = 0, $limit = 0 ){
		$conditions['class_id'] = $class_id;
		if($_POST){
			$post_data = $this->input->post();
			if(!empty($post_data['name'])){
				$name = $post_data['name'];
			}
		}
		$conditions['name'] = $name;
		$this->load->model("Courseo2o_model");
		$courses = $this->Courseo2o_model->get_class_course($limit, $this->offset, $conditions);
		$this->load->library('pagination');
		$page_config['base_url'] = '/user/classes/courseware/' . $class_id . '/' . $name;
		$page_config['total_rows'] = $courses['total_count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 6;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("conditions", $conditions);

		$this->smarty->assign("class_id", $class_id);
		$this->smarty->assign("courses", $courses);
		$this->smarty->display("user/courseware.html");
	}
}
